﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace CommonUtils
{
    /// <summary>
    /// 加密解密工具
    /// </summary>
    public static class EncryptUtil
    {
        //OracleInternal 奥睿科的尽量不用吧！
        //OracleInternal.Secure.Network.DataIntegrityAlgorithm;
        //OracleInternal.Secure.Network.DES112;
        //OracleInternal.Secure.Network.DES168;
        //OracleInternal.Secure.Network.EncryptionAlgorithm;
        //OracleInternal.Secure.Network.MD5;
        //OracleInternal.Secure.Network.SHA1;
        //OracleInternal.Secure.Network.SHA256;
        //OracleInternal.Secure.Network.SHA384;
        //OracleInternal.Secure.Network.SHA512;

        //System 还是原生的好
        //System.Security.Cryptography.Aes;
        //System.Security.Cryptography.DES;
        //System.Security.Cryptography.HMAC;
        //System.Security.Cryptography.HMACMD5;
        //System.Security.Cryptography.HMACSHA1;
        //System.Security.Cryptography.HMACSHA256;
        //System.Security.Cryptography.HMACSHA384;
        //System.Security.Cryptography.HMACSHA512;
        //System.Security.Cryptography.MD5;
        //System.Security.Cryptography.Oid;
        //System.Security.Cryptography.RSA;
        //System.Security.Cryptography.SHA1;
        //System.Security.Cryptography.SHA256;
        //System.Security.Cryptography.SHA384;
        //System.Security.Cryptography.SHA512;
        //System.Security.Cryptography.X509Certificates;

        public static string GetSHA1(byte[] bytes, bool needHead = false)
        {
            var cpu = SHA1.Create();
            var result = cpu.ComputeHash(bytes);
            return result.ToHexadecimalString(needHead).ToUpper();
        }

        public static string GetSHA1FromFile(string path, bool needHead = false)
         => GetSHA1(FileUtil.GetBytes(path), needHead);

        public static byte[] Md5Encode(byte[] data)
        => MD5.Create().ComputeHash(data);

        public static byte[] AesEncode(byte[] data)
        => Aes.Create().CreateEncryptor().TransformFinalBlock(data, 0, data.Length);

        public static byte[] AesEncode(byte[] data, string aStrKey, string iv, CipherMode mode = CipherMode.CBC)
        {
            byte[] result = null;
            try
            {
                var des = Aes.Create();
                des.Mode = mode;
                des.Key = Encoding.ASCII.GetBytes(aStrKey);
                des.IV = Encoding.ASCII.GetBytes(iv);

                var desEncrypt = des.CreateEncryptor();
                result = desEncrypt.TransformFinalBlock(data, 0, data.Length);
            }
            catch (Exception)
            {
                // no use
            }

            return result;
        }

        public static byte[] AesDecode(byte[] data)
        => Aes.Create().CreateDecryptor().TransformFinalBlock(data, 0, data.Length);

        public static byte[] AesDecode(byte[] data, string aStrKey, string iv, CipherMode mode = CipherMode.CBC)
        {
            byte[] result = null;
            try
            {
                var des = Aes.Create();
                des.Key = Encoding.ASCII.GetBytes(aStrKey);
                des.Mode = mode;
                des.Padding = PaddingMode.PKCS7;
                des.IV = Encoding.ASCII.GetBytes(iv);

                var desDecrypt = des.CreateDecryptor();
                result = desDecrypt.TransformFinalBlock(data, 0, data.Length);
            }
            catch (Exception)
            {
                // no use
            }
            return result;
        }

        public static byte[] DesEncode(byte[] data)
        => DES.Create().CreateDecryptor().TransformFinalBlock(data, 0, data.Length);

        public static byte[] DesDecode(byte[] data)
        => DES.Create().CreateDecryptor().TransformFinalBlock(data, 0, data.Length);

        public static byte[] HmacSha256Encode(byte[] seed, byte[] data)
        => new HMACSHA256(seed).ComputeHash(data);
    }
}
